package com.sunfeng.chap14;

import java.nio.ByteBuffer;

public class Test5_3 {
    public static void main(String[] args) {
        byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

        ByteBuffer byteBuffer = ByteBuffer.allocate(10);

        int arrayIndex=0;
        while (arrayIndex<bytes.length){
            int readLength = Math.min(byteBuffer.remaining(), bytes.length - arrayIndex);
            //想缓存区写数据
            byteBuffer.put(bytes,arrayIndex,readLength);
            byteBuffer.flip(); //字符串截取
            byte[] array = byteBuffer.array();

            for (int i = 0; i <byteBuffer.limit(); i++) {
                System.out.print(array[i] + " ");
            }
            arrayIndex=arrayIndex+readLength;
            System.out.println();
            byteBuffer.clear();
        }

    }
}
